package com.lmscn.lms.qsws.controller;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.lmscn.lms.api.model.Article;
import com.lmscn.lms.api.model.CourseBase;
import com.lmscn.lms.api.model.Teacher;
import com.lmscn.lms.api.util.JsonKit;
import com.lmscn.lms.commons.GlobalConst;

public class TransferController extends Controller {
	public void index(){
		renderJsp("/WEB-INF/jsp/qsws/nav.jsp");
	}
	public void toIndex(){
		renderJsp("/WEB-INF/jsp/qsws/index.jsp");
	}
	public void toArticleDetailPage(){
		Long articleId = getParaToLong("articleId");
		setAttr("article", Article.dao.findById(articleId));
		renderJsp("/WEB-INF/jsp/qsws/article_detail.jsp");
	}
	
	public void toArticleListPage(){
		renderJsp("/WEB-INF/jsp/qsws/article_list.jsp");
	}
	
	public void toTeacherDetailPage(){
		setAttr("teacherId",getPara("teacherId"));
		renderJsp("/WEB-INF/jsp/qsws/teacher_detail.jsp");
	}
	
	public void teacher(){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Long teacherId = getParaToLong("teacherId");
		dataMap.put("teacher", Teacher.dao.findById(teacherId));
		renderJson(JsonKit.mapToJson(dataMap, GlobalConst.DEFAULT_DEPTH));
	}
	
	public void coursesOfTeacher(){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		String teacherIds = getPara("teacherId");
		dataMap.put("courseList", CourseBase.dao.findByTeacher(String.valueOf(teacherIds)));
		renderJson(JsonKit.mapToJson(dataMap, GlobalConst.DEFAULT_DEPTH));
	}
	
	public void toTeacherListPage(){
		renderJsp("/WEB-INF/jsp/qsws/teacher_list.jsp");
	}
	
	public void teacherList(){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		int pageNumber = getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize");
		Page<Teacher> teachers = Teacher.dao.getTeacherPage(pageNumber, pageSize);
		dataMap.put("teacherList", teachers.getList());
		renderJson(JsonKit.mapToJson(dataMap, GlobalConst.DEFAULT_DEPTH));
	}
	
	public void toHotCoursePage(){
		renderJsp("/WEB-INF/jsp/qsws/hotCourse.jsp");
	}
	
	public void hotCourses(){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		int pageNumber = getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize");
		Page<CourseBase> courses = CourseBase.dao.getCoursePage(pageNumber, pageSize, "t.play_num DESC");
		dataMap.put("courseList", courses.getList());
		renderJson(JsonKit.mapToJson(dataMap, GlobalConst.DEFAULT_DEPTH));
	}
	
	public void teacherOfCourse(){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Long courseId = getParaToLong("courseId");
		CourseBase course = CourseBase.dao.findById(courseId);
		String teacherIds = course.getStr("teacher_ids");
		Teacher teacher = null;
		if(StrKit.notBlank(teacherIds)){
			teacher = Teacher.dao.findById(Long.valueOf(course.getStr("teacher_ids").split(",")[0]));
		}
		dataMap.put("teacher", teacher);
		renderJson(JsonKit.mapToJson(dataMap, GlobalConst.DEFAULT_DEPTH));
	}
	
	public void toCourseDetailPage(){
		renderJsp("/WEB-INF/jsp/qsws/course_detail.jsp");
	}
	
	public void toSearchPage(){
		renderJsp("/WEB-INF/jsp/qsws/search.jsp");
	}
	
	public void toSearchResultPage(){
		String keyword = getPara("keyword");
		setAttr("keyword", keyword);
		renderJsp("/WEB-INF/jsp/qsws/search_result.jsp");
	}
	
}
